# coding=utf-8
# !/usr/bin/python
# 小司机出品
import sys
sys.path.append('..')
from base.spider import Spider
import json
import base64
from urllib.parse import unquote
import re
class Spider(Spider):  # 元类 默认的元类 type
    def getName(self):
        return "站点集合"

    def init(self, extend=""):
        print("============{0}============".format(extend))
        pass

    def isVideoFormat(self, url):
        pass

    def manualVideoCheck(self):
        pass

    def homeContent(self, filter):
        result = {}
        cateManual = {
"404xav":"1",
"其他待添加":"2"
}
        classes = []
        for k in cateManual:
            classes.append({
                'type_name': k,
                'type_id': cateManual[k]
            })
        result['class'] = classes
        return result

    def homeVideoContent(self):
        result = {
            'list': []
        }
        return result
    def categoryContent(self, tid, pg, filter, extend):
        result = {}
        videos = []
        host = "https://4042409.fun"
        urla = "https://4042409.fun/list/"
        rspa = self.fetch(urla)
        roota = self.html(self.cleanText(rspa.text))
        aList = roota.xpath("//div[@class='listinfo']/ul/li")
        if tid == "1":
            for a in aList:
                name = a.xpath('./a/span/text()')[0]
                #nameb = a.xpath('./p/a/span/text()')[0]
                sid = a.xpath("./a/@href")[0]
                #sidb = a.xpath("./p/a/@href")[0]
                videos.append({
                    "vod_id":sid,
                    "vod_name":name,
                    "vod_tag":"folder"
                })
        elif not tid.startswith('https'):
            urlb = host + tid
            rspb = self.fetch(urlb)
            rootb = self.html(self.cleanText(rspb.text))
            aListb = rootb.xpath("//div[@id='type']/ul/li")
            for a in aListb:
                name = a.xpath('./a/text()')[0]
                sid = a.xpath("./a/@href")[0]
                sid = self.regStr(sid,"/list/(.*?).html")
                videos.append({
                    "vod_id":host + "/list/" + sid,
                    "vod_name":name,
                    "vod_tag":"folder"
                })
        else:
            extend['page'] = '/' + pg + '/.html'
            urlc = tid + extend['page']
            rspc = self.fetch(urlc)
            rootc = self.html(self.cleanText(rspc.text))
            aListc = rootc.xpath("//div[@id='list']/ul/li/a")
            for a in aListc:
                name = a.xpath('./span[@class="vodname"]/text()')[0]
                pic = a.xpath('./img/@data-original')[0]
                sid = a.xpath("./@href")[0]
                mark = a.xpath("./span[@class='vodtime']/text()")[0]
                videos.append({
                    "vod_id":host + sid,
                    "vod_name":name,
                    "vod_pic":pic,
                    "vod_remarks":mark
                })
        result['list'] = videos
        result['page'] = 99
        result['pagecount'] = 99
        result['limit'] = 999
        result['total'] = 999999
        return result

    def detailContent(self, array):
        id = array[0]
        result = {}
        vod = {
            "vod_id": id,
            "vod_name": "",
            "vod_pic": '',
            "vod_tag": '',
            "vod_play_from": "播放",
            "vod_play_url": id
        }
        result = {
            'list': [
                 vod
            ]
        }
        return result

    def searchContent(self, key, quick):
        result = {
            'list': []
        }
        return result

    def playerContent(self, flag, id, vipFlags):
        result = {}
        result["header"] = self.header
        result["parse"] = 1
        result["playUrl"] = ''
        result["url"] = id
        return result

    flurl = ''
    config = {
        "player": {},
        "filter": {}
    }
    header = {}

    def localProxy(self, param):
        return [200, "video/MP2T", action, ""]